package gcb.metalsys.service.cop.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gcb.metalsys.exception.UploadException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import gcb.metalsys.entity.cop.CopProduct;
import gcb.metalsys.enums.UploadEnum;
import gcb.metalsys.mapper.cop.CopProductMapper;
import gcb.metalsys.service.cop.CopProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2019-12-30
 */
@Service
public class CopProductServiceImpl extends ServiceImpl<CopProductMapper, CopProduct> implements CopProductService {

    @Override
    public void readExcel(MultipartFile multipartFile) throws RuntimeException {
        String filePath = multipartFile.getOriginalFilename();

        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream inputStream = null;
        Workbook workbook = null;
        List<CopProduct> productList = new ArrayList<>();

        try {
            inputStream = multipartFile.getInputStream();
            if (".xls".equals(extString)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (".xlsx".equals(extString)) {
                workbook = new XSSFWorkbook(inputStream);
            }

            Sheet sheet = workbook.getSheetAt(0);
            int rows = sheet.getLastRowNum() + 1;
            Row tmp = sheet.getRow(0);
            int cols = tmp.getPhysicalNumberOfCells();


            for (int row = 1; row < rows; row++) {
                Row r = sheet.getRow(row);



                CopProduct product = new CopProduct(
                        r.getCell(0).getStringCellValue(),
                        r.getCell(2).getStringCellValue(),
                        r.getCell(1).getStringCellValue(),
                        new Integer(r.getCell(7).getStringCellValue()),
                        new BigDecimal(r.getCell(4).getNumericCellValue()) ,
                        timeChange(Double.toString(r.getCell(5).getNumericCellValue())),
                        timeChange(Double.toString(r.getCell(6).getNumericCellValue())),
                        r.getCell(3).getStringCellValue(), "/tool/img/99K.jpg");
                productList.add(product);
            }
            List<String> nameList = productList.stream().map(CopProduct::getProductName).collect(Collectors.toList());
            QueryWrapper<CopProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("product_name",nameList);
            List<CopProduct> existList = list();

            for (CopProduct product : existList) {
                for (CopProduct copProduct : productList) {
                    if (copProduct.getProductName().equals(product.getProductName())) {
                        copProduct.setProductId(product.getProductId());
                        break;
                    }
                }
            }
            saveOrUpdateBatch(productList);
        } catch (Exception e) {
            throw new UploadException(UploadEnum.ERROR);
        }
    }

    private String timeChange(String time){
        String[] split = time.split("\\.");
        StringBuilder result = new StringBuilder(split[0]);
        if (result.length()==5) {
            result.insert(1,":");
            result.insert(4,":");
        }else if(result.length()==4){
            result.insert(0,"00:");
            result.insert(5,":");
        }else{
            result.insert(2,":");
            result.insert(5,":");
        }
        return result.toString();
    }
}
